package com.shenfa.util.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.ServletContextFactoryBean;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.shenfa.bean.Person;
import com.shenfa.service.systemManager.person.PersonService;

public class SessionLoginOutListener implements HttpSessionListener,ServletContextListener {

	private static Logger log = Logger.getLogger(SessionLoginOutListener.class);

	private PersonService personService;
	
	private static ApplicationContext ac;
	
	public void sessionCreated(HttpSessionEvent arg0) {
		
	}
	//session失效时调用
	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session = event.getSession(); 
		personService = (PersonService)ac.getBean("personService");
        // 获取绑定的session 
        Person per = (Person) session.getAttribute("login");
        if(null!=per&&!"".equals(per.getId())){
        	Person person = new Person();
        	person.setId(per.getId());
        	person.setS_ip("0");
			personService.logStateUp(person);
		}
	}
	public void contextDestroyed(ServletContextEvent arg0) {
		
	}
	public void contextInitialized(ServletContextEvent arg0) {
		 ac = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
	}

}
